<?php

namespace App\Crontab;

use Hyperf\Crontab\Annotation\Crontab;
use Service\AppService\IdService;
use App\Service\AppService\RedisHashLock;
use App\Service\CommodityService\SupplierCommodityHanMianPullLogService;

/**
 * 韩免直邮供应商拉取商品
 * @Crontab(name="crontab_commodity_hm_pull_goods", rule="*\/5 * * * * *", callback="execute", enable="isEnable", memo="韩免直邮供应商拉取商品")
 */
class HanMianCrontab
{
    protected $redis_lock_key = 'crontab_commodity_hm_pull_goods';
    protected $redis_key = 'pull_goods';
    
    /**
     * 开始执行业务脚本
     */
    public function execute()
    {
        try {
            $kVal   = IdService::instance()->getOtherID();
            $isLock = RedisHashLock::instance()->getLockHash($this->redis_key,$this->redis_lock_key,$kVal);
            if( $this->isEnable() && $isLock ) {
                echo "{$this->redis_lock_key} is run pull goods now \r\n";
                waiter(function(){
                    SupplierCommodityHanMianPullLogService::instance()->runTask('shophanmian');
                },(int)env('crontab_commodity_xyxb_pull_goods_wait_time'));
                RedisHashLock::instance()->unLockHash($this->redis_key,$this->redis_lock_key,$kVal);
            } else {
                echo "{$this->redis_lock_key} is run pull goods is InVolid \r\n";
            }
        } catch ( \Throwable $th ) {
            echo "{$this->redis_lock_key} is pull goods is error now ，{$th->getMessage()},{$th->getFile()},{$th->getLine()} \r\n";
            RedisHashLock::instance()->unLockHash($this->redis_key,$this->redis_lock_key,$kVal);
        }
    }
    
    /**
     * 是否开启可用
     */
    public function isEnable(): bool
    {
        return env("is_enable_{$this->redis_lock_key}",false);
    }
}